<?php 
namespace Home\Controller;
use Home\Controller\HomeController;
use Common\Api\SendPhoneCodeApi;
use Common\Model\UserRelationModel;
use Common\Model\MemberModel;
use Common\Model\BannerModel;
class UserController extends HomeController {
	protected $user;
    //空操作
    public function _initialize(){
    	//检测是否登录
    	parent::_initialize();
    	$token = $_SERVER['HTTP_TOKEN'];
    	if(empty($token)){
    	    $token = I('token')?I('token'):'';
    	}
    	$user = $this->checkUser($token);
    	$this->user = $user;
    	$this->uid = $user['member_id'];
        parent::_initialize();
    }
    public function getIndexInfo(){
    	$BANNER = new BannerModel();
    	$banner_where['status'] = 1 ;
    	$banner_where['type'] = I('type')?I('type'):'1' ;
    	$banner_list = $BANNER->selectBanner(8,$banner_where);
    	$return['banner'] = $banner_list;
    	$art=M('article')->where('status = 1')->select();
    	$return['article'] = $art;
    	$cu=M('member_money')
    	->alias('m')
    	->field('m.*,c.currency_name,c.currency_mark,c.currency_logo')
    	->join('__CURRENCY__ as c on c.currency_id = m.currency_id')
    	->where('m.uid ='.$this->uid)
    	->select();
    	foreach ($cu as $k=>$v){
    		$price=M('price')->find();
    		$cu[$k]['price']=$price['price'.$v['currency_id']];
    	}
    	$return['currency'] = $cu;
    	$return['status'] = 1;
    	return $this->ajaxReturn($return);
    }
    public function getLiziInfo(){
    	$BANNER = new BannerModel();
    	$banner_where['status'] = 1 ;
    	$banner_where['type'] = I('type')?I('type'):'1' ;
    	$banner_list = $BANNER->selectBanner(8,$banner_where);
    	$return['banner'] = $banner_list;
    	$art=M('article')->where('status = 1')->select();
    	$return['article'] = $art;
    	$cu=M('member_money')
    	->alias('m')
    	->field('m.*,c.currency_name,c.currency_mark,c.currency_logo')
    	->join('__CURRENCY__ as c on c.currency_id = m.currency_id')
    	->where('m.currency_id != 5')
    	->select();
    	$return['currency'] = $cu;
    	$return['status'] = 1;
    	return $this->ajaxReturn($return);
    }
    //检测用户token
    protected function checkUser($token) {
    	//下面是针对登录注册 手机版的检测方法
    	$where_token['token'] = $token;
    	$token_log = M('Token')->where($where_token)->find();
    	if(!$token_log){
    		$data_return['status'] = -998;
    		$data_return['info'] = '请重新登录';
    		return $this->ajaxReturn($data_return);
    	}
    	$where_user['member_id'] = $token_log['uid'];
    	$where_user['status'] = array('neq',-404);
    	$user = M('Member')->where($where_user)->find();
    	if(!$user){
    		$data_return['status'] = -998;
    		$data_return['info'] = '查无此人';
    		return $this->ajaxReturn($data_return);
    	}
    	return $user;
    }
    public function index(){
       $memeber=$this->user;
       $data['user']=$memeber;
       $data['config']=$this->config;
       $data['status']=1;
       $this->ajaxReturn($data);
    }
    public function getCode(){
        $invitation_code = $this->getCodeByUid($this->uid);
        //我的邀请链接
        if(!$this->user['ercode']){
        	$link = "http://".$_SERVER['SERVER_NAME']."/home/Reg/register?code=".$invitation_code;
        	//我的二维码
        	$ercode = $this->qrcode($link, '', 'ercode');
        	M('member')->where('member_id ='.$this->uid)->setField('ercode',$ercode);
        }else{
        	$ercode =$this->user['ercode'];
        }
        $list['code'] = $ercode;
        $list['invitation_code'] = $invitation_code;
        $list['status'] = 1;
        $this->ajaxReturn($list);
    }
    public function getCodeByUid($uid){
        return M('Invitation_code')->where(['uid'=>$uid])->find()['unique_code'];
    }
    public function userRelation(){
        $list = M('User_relation')->where(['uid'=>$this->uid])->find();
        $this->ajaxReturn($list);
    }
  
    /**
     * 修改账号密码
     */
    public function updatePassword(){
        $pwd=I('pwd');
        $newpwd=I('newpwd');
        $repwd=I('repwd');
        if($newpwd!=$repwd){
            $data['status']=0;
            $data['info']='两次密码不一致';
            $this->ajaxReturn($data);
        }
        $member=M('Member')->where(['member_id'=>$this->user['member_id']])->find();
        if(md5($pwd)!=$member['pwd']){
			$data['status']=0;
			$data['info']='原密码错误';
			$this->ajaxReturn($data);
        }
        if($newpwd==$pwd){
        	$data['status']=0;
        	$data['info']='新密码与旧密码相同，不需要重复操作';
        	$this->ajaxReturn($data);
        }
        if(!regex($newpwd,'password')){
    		$data_return['status'] = -9;
    		$data_return['info'] = '请输入6~20位数字字母组成的密码';
    		return $this->ajaxReturn($data_return);
    	}
        $arr['pwd']=md5($newpwd);
        $r=M('Member')->where(['member_id'=>$this->user['member_id']])->save($arr);
        if($r){
        	$data['status']=1;
        	$data['info']='修改成功';
        	$this->ajaxReturn($data);
        }else{
        	$data['status']=0;
        	$data['info']='服务器繁忙，请稍后再试';
        	$this->ajaxReturn($data);
        }
    }
    /**
     * 设置过的修改密码
     */
    public function changePwdTrade(){
    	$newpwd=I('newpwd');
    	$repwd=I('repwd');
    	$code=I('code');
    	$member=M('Member')->where(['member_id'=>$this->user['member_id']])->find();
    	if($newpwd!=$repwd){
    	    $data['status']=0;
    	    $data['info']='两次密码不一致';
    	    $this->ajaxReturn($data);
    	}
    	if($code != session('codexiugaijiaoyi'.$this->user['phone'])){
    		$data_return['status'] = -13;
    		$data_return['info']   = '手机验证码有误';
    		$this->ajaxReturn($data_return);
    	}
    	if(!regex($newpwd,'password')){
    	    $data_return['status'] = -9;
    	    $data_return['info'] = '请输入6~20位数字字母组成的密码';
    	    return $this->ajaxReturn($data_return);
    	}
    	if(md5($newpwd)==$this->user['pwdtrade']){
    		$data['status']=0;
    		$data['info']='新密码与旧密码相同，不需要重复操作';
    		$this->ajaxReturn($data);
    	}
    	$arr['pwdtrade']=md5($newpwd);
    	$r=M('Member')->where(['member_id'=>$this->user['member_id']])->save($arr);
    	if($r){
    		$data['status']=1;
    		$data['info']='修改成功';
    		$this->ajaxReturn($data);
    	}else{
    		$data['status']=0;
    		$data['info']='服务器繁忙，请稍后再试';
    		$this->ajaxReturn($data);
    	}
    }
    public function sendPhone(){
    	$code_name = 'xiugaijiaoyi';
    	$SENDPHONE = new SendPhoneCodeApi($this->user['phone']);
    	$send = $SENDPHONE->send($code_name);
    	if($send == 1){
    		$data_return['status'] = 1;
    		$data_return['info']   = '发送成功';
    		return $this->ajaxReturn($data_return);
    	}else{
    		$data_return['status'] = -1;
    		$data_return['info']   = $send;
    		return $this->ajaxReturn($data_return);
    	}
    }
 

    //显示团队信息
    public function getTeamList(){
    	$relation=M('User_relation')->where('uid ='.$this->uid)->find();
    	$price=M('price')->find();
    	$yeji=$relation['num1']*$price['price1']+$relation['num2']*$price['price2']+$relation['num3']*$price['price3']+$relation['num4']*$price['price4'];
    	$return['yeji']=round($yeji,2);
    	$return['level']=$this->user['level'];
    	$ur=$this->user['relation'];
    	$where['relation']=array('like',$this->user['relation'].'%');
    	$list=M('member')->where($where)->select();
        foreach ($list as $k=>$v){
        	$list[$k]['daishu']=(strlen($v['relation'])-strlen($ur))/3;
            $list[$k]['phone'] = substr($list[$k]['phone'], 0, 3).'****'.substr($list[$k]['phone'], 7);           
        }
        $return['data'] = $list;
        $return['status'] = 1;
        $this->ajaxReturn($return);
    }
 
    public function getMyProblem(){
    	$list=M('Question')->where(['uid'=>$this->uid])->select();
    	$data['list']=$list;
    	$data['status']=1;
    	$this->ajaxReturn($data);
    }
    /**
     * 我的糖果
     */
    public function getSweetLog(){
    	$list=M('sweet_log')->where('uid ='.$this->uid)->select();
    	$data['list']=$list;
    	$data['status']=1;
    	$this->ajaxReturn($data);
    }
    /**
     * 联系我们
     */
    public function contract(){
    	$keyword=I('keyword');
    	$where['keyword']=array('like','%'.$keyword.'%');
    	$list=M('answer')->where($where)->select();
    	$data['list']=$list;
    	$data['status']=1;
    	$this->ajaxReturn($data);
    }
    /**
     * 问题反馈
     */
    public function addProblem(){
    	$uid = $this->uid;
    	$log = M('Question')->where(['status'=>0,'uid'=>$uid])->count();
    	if($log>10){
    		$data['status'] = -2;
    		$data['info'] = '请勿频繁提交留言';
    		$this->ajaxReturn($data);
    	}
    	$title = I('title');
    	$content = I('content');
    	if(empty($title)){
    		$data['status'] = -1;
    		$data['info'] = '请输入留言标题';
    		$this->ajaxReturn($data);
    	}
    	if(empty($content)){
    		$data['status'] = -1;
    		$data['info'] = '请输入留言内容';
    		$this->ajaxReturn($data);
    	}
    	$data['uid'] = $uid;
    	$data['title'] = $title;
    	$data['content'] = $content;
    	$data['add_time'] = time();
    	$r = M('Question')->add($data);
    	if($r){
    		$data['info']   = '留言成功';
    		$data['status'] = 1;
    		$this->ajaxReturn($data);
    	}else{
    		$data['info']   = '系统繁忙';
    		$data['status'] = '-1';
    		$this->ajaxReturn($data);
    	}
    }
    public function qrcode($address,$logo1,$name) {
    	Vendor('phpqrcode.phpqrcode');//这个很重要，没引用则不能使用
    	$value = $address; //二维码内容
    	$errorCorrectionLevel = 'L'; //容错级别
    	$matrixPointSize = 4; //生成图片大小
    	//生成二维码图片
    	\QRcode::png($value, 'qrcode.jpg', $errorCorrectionLevel, $matrixPointSize, 2);//之前做的时候少了一个'\'，会显示没有此类
    	$logo = '.'.$logo1; //准备好的logo图片 ；引用文件地址同样很重要，我犯了引入地址错误，结果最后的二维码中间没有图片
    	$QR = 'qrcode.jpg'; //已经生成的原始二维码图
    	if ($logo !== FALSE) {
    		$QR = imagecreatefromstring(file_get_contents($QR));
    		$logo = imagecreatefromstring(file_get_contents($logo));
    		$QR_width = imagesx($QR);//二维码图片宽度
    		$QR_height = imagesy($QR);//二维码图片高度
    		$logo_width = imagesx($logo);//logo图片宽度
    		$logo_height = imagesy($logo);//logo图片高度
    		$logo_qr_width = $QR_width / 5;
    		$scale = $logo_width/$logo_qr_width;
    		$logo_qr_height = $logo_height/$scale;
    		$from_width = ($QR_width - $logo_qr_width) / 2;
    		//重新组合图片并调整大小
    		imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
    		$logo_qr_height, $logo_width, $logo_height);
    	}
    	$url= './ercode/'.$name.'.png';
    	//输出图片
    	imagepng($QR, $url);
    	return substr($url, 1);
    }
    public function getProfit(){
    	$list=M('profit')->where('uid ='.$this->uid)->find();
    	$today=$list['jj_num']+$list['sc_num']+$list['fx_num']+$list['sq_num']+$list['point_num']+$list['global_num'];
    	$price=M('price')->find()['price5'];
    	$list['today']=$today;
    	$list['today_money']=round($today*$price,2);
    	$list['all']=$list['all_num'];
    	$list['all_money']=round($list['all_num']*$price,2);
    	$data['list']=$list;
    	$data['status']=1;
    	$this->ajaxReturn($data);
    }
}